@model EIMS.Models.ViewModels.CustomerTransactionViewModel

@{
    ViewData["Title"] = "编辑交易记录";
}

<div class="container-fluid px-4">
    <h1 class="mt-4">@ViewData["Title"]</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li class="breadcrumb-item"><a asp-controller="Customer" asp-action="Index">客户管理</a></li>
        <li class="breadcrumb-item"><a asp-controller="Customer" asp-action="Details" asp-route-id="@Model.CustomerId">客户详情</a></li>
        <li class="breadcrumb-item active">编辑交易记录</li>
    </ol>
    
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-money-check-alt me-1"></i>
            编辑 <strong>@Model.CustomerName</strong> 的交易记录
        </div>
        <div class="card-body">
            <form asp-action="EditTransaction" method="post">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <input type="hidden" asp-for="Id" />
                <input type="hidden" asp-for="CustomerId" />
                <input type="hidden" asp-for="CustomerName" />
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="TransactionNumber" class="control-label"></label>
                            <input asp-for="TransactionNumber" class="form-control" readonly />
                            <span asp-validation-for="TransactionNumber" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="Type" class="control-label"></label>
                            <select asp-for="Type" asp-items="ViewBag.TransactionTypes" class="form-select"></select>
                            <span asp-validation-for="Type" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="TransactionDate" class="control-label"></label>
                            <input asp-for="TransactionDate" class="form-control" type="date" />
                            <span asp-validation-for="TransactionDate" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="Amount" class="control-label"></label>
                            <div class="input-group">
                                <span class="input-group-text">¥</span>
                                <input asp-for="Amount" class="form-control" type="number" step="0.01" min="0.01" />
                            </div>
                            <span asp-validation-for="Amount" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="PaymentStatus" class="control-label"></label>
                            <select asp-for="PaymentStatus" asp-items="ViewBag.PaymentStatuses" class="form-select"></select>
                            <span asp-validation-for="PaymentStatus" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="PaymentDate" class="control-label"></label>
                            <input asp-for="PaymentDate" class="form-control" type="date" />
                            <span asp-validation-for="PaymentDate" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="OrderId" class="control-label"></label>
                            <input asp-for="OrderId" class="form-control" type="number" />
                            <span asp-validation-for="OrderId" class="text-danger"></span>
                            <small class="form-text text-muted">可选，关联订单编号</small>
                        </div>
                    </div>
                </div>
                
                <div class="form-group mb-3">
                    <label asp-for="Remarks" class="control-label"></label>
                    <textarea asp-for="Remarks" class="form-control" rows="3"></textarea>
                    <span asp-validation-for="Remarks" class="text-danger"></span>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 保存
                    </button>
                    <a asp-action="Details" asp-route-id="@Model.CustomerId" class="btn btn-secondary">
                        <i class="fas fa-arrow-left"></i> 返回
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    
    <script>
        $(document).ready(function() {
            // 付款状态与付款日期联动
            $('#PaymentStatus').change(function() {
                if ($(this).val() == '0') { // Unpaid
                    $('#PaymentDate').val('').prop('disabled', true);
                } else {
                    $('#PaymentDate').prop('disabled', false);
                    if ($('#PaymentDate').val() == '' && ($(this).val() == '1' || $(this).val() == '2')) { // Paid or PartiallyPaid
                        $('#PaymentDate').val(new Date().toISOString().split('T')[0]);
                    }
                }
            });
            
            // 初始触发一次
            $('#PaymentStatus').trigger('change');
        });
    </script>
}